#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
from generate.generator import BasicGenerator

import logging


'''
task = {
    'type': 'download_xx',               # 必填，任务类型
    'url': 'http://www.xx.com/id.html'   # 必填，url地址， get请将参数放到url中
    'file': './data/download_xx/id.html' # 选填, 是否要存成文件
    'duplicated': False,                 # 选填, False为不去重
    'duplicated_key': 'xx',              # 选填, 去重的key，一般不使用
    'method': 'post',                    # 选填, post接口用
    'params': {'a':'b', 'c':'d'}         # 选填, 传参数
    ...                                  # 其它自定义参数
    'data'                               # data 已经预留，不允许使用
    'expire_time'                        # 选填, 传去重过期时间
}

config = {
    'name'
    'mq'
    'stat'
    'proxy'
    'mdb'
    'analyze_type'
}
'''


class QheeProjectListGenerator(BasicGenerator):
    def gen_task(self, extra=None):
        url = 'https://www.qhee.com/node/proxy-actionV2/qheePolicy/allSearch/v2'
        params = {"partnerChannel": "1", "pageNo": "1", "pageSize": "10", "isOwner": "0", "_action": "/qheePolicy/allSearch/v2"}
        # cookie = '__utmz=232670943.1603262432.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ' \
        #          '__utmv=232670943.|1=%E8%AE%BF%E9%97%AE%E7%94%A8%E6%88%B7%E7%B1%BB%E5%9E%8B=%E6%B8%B8%E5%AE%A2=1; ' \
        #          'login_name=13651173927; __user_session_id_=97d9c7c1-f33b-4205-bc48-52cc2e6fa33b; ' \
        #          'Hm_lvt_d900963e294195855cd1da3cb5ad2f69=1603330844,1603419012,1603675040,1603761543; ' \
        #          '__utmc=232670943; __qhee_sid__="1q16ZMVw65JGskKasdkhPwBhQ1UvwTQEqGRGeeIEeVE="; ' \
        #          'oscid=hamHH3zMGOa8T4qDTxb1IfP1LxaeS3XRjVLTMlzPvsxTBOLdOHfktxtHn02cLAn2; ' \
        #          '__utma=232670943.1956844714.1603262432.1603765808.1603768225.17; ' \
        #          'Hm_lpvt_d900963e294195855cd1da3cb5ad2f69=1603768641 '
        task = {
            'type': self.name,
            'url': url,
            'method': 'post',
            'params': params,
            # 'cookie': cookie,

        }
        length = self.mq.push(self.queue_name, json.dumps(task))
        logging.info('{0} {1}'.format(length, task))
        self.stat.incr(self.stat_hash, 'gen_task')


class QheeProjectGenerator(BasicGenerator):
    def get_project(self):
        sql = '''select project_id from qhee_project_list;'''
        rows = self.mdb.query(sql)
        list = []
        for row in rows:
            list.append(row)
        return list

    def gen_task(self, extra=None):
        projects = self.get_project()
        for project_id in projects:
            url = 'https://www.qhee.com/node/zct/policy-detail/' + str(project_id[0])
            cookie = '__utmz=232670943.1603262432.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ' \
                     '__utmv=232670943.|1=%E8%AE%BF%E9%97%AE%E7%94%A8%E6%88%B7%E7%B1%BB%E5%9E%8B=%E6%B8%B8%E5%AE%A2=1' \
                     '; login_name=13651173927; __user_session_id_=97d9c7c1-f33b-4205-bc48-52cc2e6fa33b; ' \
                     'Hm_lvt_d900963e294195855cd1da3cb5ad2f69=1603330844,1603419012,1603675040,1603761543; ' \
                     '__utmc=232670943; __qhee_sid__="1q16ZMVw65JGskKasdkhPwBhQ1UvwTQEqGRGeeIEeVE="; ' \
                     'oscid=hamHH3zMGOa8T4qDTxb1IfP1LxaeS3XRjVLTMlzPvsxTBOLdOHfktxtHn02cLAn2; ' \
                     '__utma=232670943.1956844714.1603262432.1603765808.1603768225.17; ' \
                     'Hm_lpvt_d900963e294195855cd1da3cb5ad2f69=1603768641 '
            task = {
                'type': self.name,
                'url': url,
                'project_id': project_id[0],
                'cookie': cookie,
                # 'file': './data/download_qhee/{0}.html'.format(str(project_id[0]))
            }
            length = self.rds.rpush(self.queue_name, json.dumps(task))
            logging.info('{0} {1}'.format(length, task))
            self.stat.incr(self.stat_hash, 'gen_task')
